function set_bitrate(bitrate) {
	_setcookie('bitrate', bitrate);
}

function _setcookie(name, val) {
	var d = new Date();
	d.setTime(d.getTime() + (365*24*60*60*10000));
	var expires = "expires="+d.toGMTString();
	document.cookie = name + '=' + val + "; expires=" + d.toGMTString() + "; domain=.www.xh69.us; path=/";
}

$(document).ready(function() {
	 FastClick.attach(document.body);

	$(".dropdown-select > button, .setting > a").click(function(e) {
		$(this).parent().toggleClass("open");
		e.preventDefault();
		return false;
	});

	$('body').on("click", function(e){
		if ($(e.target).parents().hasClass("dropdown-select")) return;
		if ($(e.target).parents().filter('.dropdown-select.open').length != 1) {
			$('.dropdown-select.open').removeClass('open');
		}
	});

	$('body').on("click", function(e){
		if ($(e.target).parents().hasClass("setting")) return;
		if ($(e.target).parents().filter('.setting.open').length != 1) {
			$('.setting.open').removeClass('open');
		}
	});

	$(".form-group").focusin(function(){
		$(this).addClass("open");
	});

	$(".form-group").focusout(function(){
		$(this).removeClass("open");
	});

	/*
	$('.formPopup > form').on('submit', function(event) {
		$('[type=text]').each(function() {
		if(!$(this).val().length) {
			event.preventDefault();
			$('.formPopup > form > input[type=text]').addClass('error');
			}
		});
	}); */

	$('.jfilestyle').jfilestyle({
		buttonText : ''
	});

	$('.set_bitrate').click(function(e){
		var b = $(this).attr('bitrate');
		set_bitrate( $(this).attr('bitrate') );
		setTimeout(function(){
			document.location.reload(true);
		}, 100);
		e.preventDefault();
		return false;
	});


});

// scroll body to 0px on click
$(window).scroll(function () {
	var fixHeaderPos = 20;
	if ($(".box-video").length) {
		fixHeaderPos = $(".box-video").offset().top + $(".box-video").height();
		$('.header').addClass("static");
	}
	if ($(this).scrollTop() > fixHeaderPos) {
		$('.header').addClass('scroll');
		$('.header.static').removeClass('static');
	} else {
		$('.header').removeClass('scroll');
	}
});

var bpopup = null;
//FlexSlider
$(document).ready(function() {

	$(".owl-carousel").each(function(){
		$(this).owlCarousel({
			itemsCustom : [ [600, 3], [1000, 5], [1600, 6] ],
			navigation : true
		});
	});

	$(".owl-carousel-static").each(function(){
		$(this).owlCarousel({
			itemsCustom : [ [600, 3], [1000, 5], [1600, 6] ],
			navigation : false
		});
	});

	$('.btnForm').bind('click', function(e) {
		e.preventDefault();
		bpopup = $('#popup-box-1').bPopup();
	});
	$('.btnAllTags').bind('click', function(e) {
		e.preventDefault();
		bpopup = $('#popup-box-2').bPopup();
	});

	$('.mobile-link').click(function(){
		_setcookie('is_mobile', 1);
		window.location.href=$(this).attr('href');
	});

	$('.desktop-link').click(function(){
		//_setcookie('is_mobile', 0);
		//window.location.href=$(this).attr('href');
	});

	function update_thumbs() {
		setTimeout(function(){
			var thumb_width = $('.owl-item').eq(0).width();	
			var thumb_height = $('.owl-item').eq(0).height();
			$('.videos-container .thumb-item').css({width: thumb_width+'px', height: thumb_height + 'px'});
			$("img.lazy").lazyload({
				threshold : 400,
				failure_limit : 15
			});
		}, 250);
	}
	$(window).on('resize', function(){
		update_thumbs();
	});
	update_thumbs();

	$('.owl-wrapper .thumb img').eq(0).load(function(){
		update_thumbs();
	});
});

